﻿/**************************************************
*基本数据访问层
 * 数据访问层  
 * 包含crud的公共方法
 * Author：JXL
 
 *************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using HouseCommon;

namespace DAL
{
    public  abstract class BaseDAL
    {
        /// <summary>
        /// 表名
        /// </summary>
        protected abstract string tablename { get; }
        /// <summary>
        /// 主键名
        /// </summary>
        protected abstract Dictionary<string, bool> pkname { get; }


        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public virtual int insert<T>(T t) where T:class
        {
            var str = Common.sqltostring(t, tablename, pkname);
            return DBhelper.NonQuery(str);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="w"></param>
        /// <returns></returns>
        public virtual int delete(string w)
        {
            var sqlstr = "delete from " + tablename + " where projectNumber in (" + w + ")";
            return DBhelper.NonQuery(sqlstr);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <param name="w"></param>
        /// <returns></returns>
        public virtual int update<T>(T t, string w) where T : class
        {
            var sqlstr = Common.updatetostring(t, w, tablename, pkname);
            return DBhelper.NonQuery(sqlstr);
            
        }
        /// <summary>
        /// 修改根据项目号
        /// </summary>
        /// <param name="t"></param>
        /// <param name="w"></param>
        /// <returns></returns>
        public virtual int updates<T>(T t, string w) where T : class
        {
            var sqlstr = Common.updatetostrings(t, w, tablename, pkname);
            return DBhelper.NonQuery(sqlstr);

        } 
        /// <summary>
        /// 获取单个的实体应用于勘察的各个表
        /// </summary>
        /// <typeparam name="T">返回实体</typeparam>
        /// <param name="t">反射需要的实体名称</param>
        /// <param name="where">条件“项目号=id”</param>
        /// <returns></returns>
        public virtual T getmodel<T>(string where) where T:class,new()
        {
            var sqlstr = Common.gettostring<T>(where, tablename);
            return DBhelper.DBHelper.GetList<T>(sqlstr).FirstOrDefault();
        }
        
    }
}
